package com.zyzy.leftfourdead.fragment.groupbuy;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.adapter.groupBuyAdapter.PrizeRecyclerViewPagerFragmentAdapter;
import com.zyzy.leftfourdead.adapter.groupBuyAdapter.PrizeViewPagerFragmentAdapter;
import com.zyzy.leftfourdead.bean.PrizeBean;
import com.zyzy.leftfourdead.helper.OkHttpClientHelper;
import com.zyzy.leftfourdead.helper.UrlHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by loverforever on 2016/8/24.
 */
public class PrizeFragment extends Fragment{
    //上下文对象
    private Context context;
    //PullToRefreshScrollView对象
    private PullToRefreshScrollView scrollView_preferenceFragment_groupBuy;
    //viewPager对象
    private ViewPager viewPager_prizeFragment_groupBuy;
    //recyclerView的对象
    private RecyclerView recyclerView_prizeFragment_groupBuy;
    //当滑到最下面的时候 显示  没有更多了
    private TextView textView_prizeFragment_groupBuy;
    //recyclerView的适配器
    private PrizeRecyclerViewPagerFragmentAdapter prizeRecyclerViewPagerFragmentAdapter;
    //viewpager的适配器
    private PrizeViewPagerFragmentAdapter prizeViewPagerFragmentAdapter;
    //创建handler对象
    private Handler handler=new Handler();
    //创建PrizeBean.BannersBean集合
    private List<PrizeBean.BannersBean>bannersBeanList=new ArrayList<>();
    //创建PrizeBean.ProductsBean的集合
    private  List<PrizeBean.ProductsBean>productsBeanList=new ArrayList<>();


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=getContext();
        prizeRecyclerViewPagerFragmentAdapter=new PrizeRecyclerViewPagerFragmentAdapter(context,productsBeanList);
        prizeViewPagerFragmentAdapter=new PrizeViewPagerFragmentAdapter(context,bannersBeanList);
        loadDataFromNet();
    }

    private void loadDataFromNet() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //URL_PRIZE
                try {
                    //获取抽奖团的网址
                    String prizeString= OkHttpClientHelper.getStringFromURL(context, UrlHelper.URL_PRIZE,"URL_PRIZE");
                    //解析json数据
                    PrizeBean prizeBean=prizeViewPagerFragmentAdapter.parseJsonToList(prizeString);
                    bannersBeanList=prizeBean.getBanners();
                    productsBeanList=prizeBean.getProducts();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            prizeViewPagerFragmentAdapter.reloadGridView(bannersBeanList,true);
                            prizeRecyclerViewPagerFragmentAdapter.reloadRecyclerView(productsBeanList,true);
                            scrollView_preferenceFragment_groupBuy.onRefreshComplete();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.prize_fragment,container,false);
        scrollView_preferenceFragment_groupBuy= (PullToRefreshScrollView) view.findViewById(R.id.scrollView_preferenceFragment_groupBuy);
        viewPager_prizeFragment_groupBuy= (ViewPager) view.findViewById(R.id.viewPager_prizeFragment_groupBuy);
        recyclerView_prizeFragment_groupBuy= (RecyclerView) view.findViewById(R.id.recyclerView_prizeFragment_groupBuy);
        textView_prizeFragment_groupBuy= (TextView) view.findViewById(R.id.textView_prizeFragment_groupBuy);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置适配器
        viewPager_prizeFragment_groupBuy.setAdapter(prizeViewPagerFragmentAdapter);
        recyclerView_prizeFragment_groupBuy.setAdapter(prizeRecyclerViewPagerFragmentAdapter);
    //设置布局管理器
        recyclerView_prizeFragment_groupBuy.setLayoutManager(new LinearLayoutManager(context));
        scrollView_preferenceFragment_groupBuy.setMode(PullToRefreshBase.Mode.BOTH);
        scrollView_preferenceFragment_groupBuy.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                //重新加载数据
                loadDataFromNet();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                //设置到底部停止刷新
                textView_prizeFragment_groupBuy.setText("没有更多了...");
                //停止刷新
                scrollView_preferenceFragment_groupBuy.onRefreshComplete();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
